home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / testtcpclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-05  |  5.0 KB  |  270 lines  |  [TEXT/MPS ]

  1. #include <Events.h>
  2. #include <memory.h>
  3. #include <types.h>
  4. #include <OSUtils.h> /* for SysBeep */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #include <sys/errno.h>
  11. #include <sys/socket.h>
  12. #include <sys/ioctl.h>
  13. #include <netinet/in.h>
  14. #include <sys/uio.h>
  15.  
  16. #include "tcpglue.h"
  17. #include "socket.internal.h"
  18.  
  19. static struct timeval selectPoll = {0,0};
  20. main()
  21. {
  22.     int status;
  23.     int s;
  24.     struct sockaddr_in me, her, name;
  25.     int namelen;
  26.     int herlen = sizeof(her);
  27.     int bytes;
  28.     char line[1000];
  29.     int count,readfds, writefds, exceptfds;
  30.     int i;
  31.     
  32. #if 1
  33.     dprintf("address %08x\n",xIPAddr());
  34.     dprintf("netmask %08x\n",xNetMask());
  35.     dprintf("max mtu %d\n",xMaxMTU());
  36. #endif
  37.  
  38.     /* make our socket */
  39.     s = s_socket(AF_INET, SOCK_STREAM, 0);
  40.     if (s < 0)
  41.     {
  42.         dprintf("socket - error %d",errno);    
  43.         exit(1);
  44.     }
  45.     
  46.     /* bind it to port zero - ie. let MacTCP pick a port */
  47.     bzero((char *)&me, sizeof(me));
  48.     me.sin_family = AF_INET;
  49.     me.sin_port = htons(0);
  50.     if (s_bind(s, (caddr_t)&me, sizeof(me), 0) < 0)
  51.     {
  52.         dprintf("bind - error %d",errno);    
  53.         exit(1);
  54.     }
  55.     
  56.     /* go non-blocking */
  57.     if (s_ioctl(s,FIONBIO,0) < 0)
  58.     {
  59.         dprintf("ioctl(FIONBIO) - error %d",errno);    
  60.         exit(1);
  61.     }
  62.  
  63.     /* connect to the server */
  64.     her.sin_family = AF_INET;
  65. #if 1
  66.     her.sin_addr.s_addr = 0x8064660a; /* madhaus */
  67. #else
  68.     her.sin_addr.s_addr = 0x0d000ce8; /* xerox.com */
  69. #endif
  70.     her.sin_port = htons(25); /* SMTP */
  71.     if (s_connect(s,(caddr_t)&her,sizeof(her)) < 0)
  72.     {
  73.         if (errno != EINPROGRESS)
  74.         {
  75.             dprintf("connect - error %d",errno);    
  76.             exit(1);
  77.         }
  78.     }
  79.     s = s;
  80.  
  81.     /* wait for the server's greeting */
  82.     for (;;)
  83.     {
  84.         tcpCheckNotify();
  85.         readfds = 0xffffffff;
  86.         count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
  87.         if (count < 0)
  88.         {
  89.             dprintf("select - error %d",errno);
  90.             exit(1);
  91.         }
  92.         if (count > 0)
  93.             break;
  94.     }
  95.  
  96.     /* read what he said */
  97.     tcpCheckNotify();
  98.     bytes = s_read(s,line,sizeof(line)-2);
  99.     if (bytes < 0)
  100.     {
  101.         dprintf("read - error %d",errno);
  102.         exit(1);
  103.     }
  104.     dprintf("read got %d bytes\n",bytes);
  105.     line[bytes] = '\0';
  106.     dprintf("'%s'\n",line);
  107.  
  108.     /* send our own introduction (using writev just for fun) */
  109.     tcpCheckNotify();
  110.     {
  111.         struct iovec iov[10];
  112.         char a[100],b[100],c[100],d[100];
  113.         
  114.         strcpy(a,"HELO milligan.");
  115.         iov[0].iov_len = strlen(a);
  116.         iov[0].iov_base = a;
  117.         
  118.         strcpy(b,"utcs.");
  119.         iov[1].iov_len = strlen(b);
  120.         iov[1].iov_base = b;
  121.         
  122.         strcpy(c,"utoronto.");
  123.         iov[2].iov_len = strlen(c);
  124.         iov[2].iov_base = c;
  125.         
  126.         strcpy(d,"ca\015\012");
  127.         iov[3].iov_len = strlen(d);
  128.         iov[3].iov_base = d;
  129.         
  130.         bytes = s_writev(s,&iov[0],4);
  131.         if (bytes <= 0 && errno != EINPROGRESS)
  132.         {
  133.             dprintf("writev - error %d",errno);
  134.             exit(1);
  135.         }
  136.     }
  137.     
  138.     /* wait for it to get there */
  139.     for (;;)
  140.     {
  141.         tcpCheckNotify();
  142.         writefds = 0xffffffff;
  143.         count = s_select(32, NULL, &writefds, NULL, &selectPoll);
  144.         if (count < 0)
  145.         {
  146.             dprintf("select - error %d",errno);
  147.             exit(1);
  148.         }
  149.         if (count > 0)
  150.             break;
  151.     }
  152.  
  153.     /* wait for him to decide he likes us */
  154.     for (;;)
  155.     {
  156.         tcpCheckNotify();
  157.         readfds = 0xffffffff;
  158.         count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
  159.         if (count < 0)
  160.         {
  161.             dprintf("select - error %d",errno);
  162.             exit(1);
  163.         }
  164.         if (count > 0)
  165.             break;
  166.     }
  167.  
  168.     /* read his approval message (or disapproval...) */
  169.     tcpCheckNotify();
  170.     bytes = s_read(s,line,sizeof(line)-2);
  171.     if (bytes < 0)
  172.     {
  173.         dprintf("read - error %d",errno);
  174.         exit(1);
  175.     }
  176.     dprintf("read got %d bytes\n",bytes);
  177.     line[bytes] = '\0';
  178.     dprintf("'%s'\n",line);
  179.     
  180.     /* a long message to test the buffer allocator */
  181.     tcpCheckNotify();
  182.     for (i=0; i<697; i++)
  183.         line[i] = 'a';
  184.     line[698] = '\015';
  185.     line[699] = '\012';
  186.     bytes = s_write(s,line,700);
  187.     if (bytes <= 0 && errno != EINPROGRESS)
  188.     {
  189.         dprintf("write - error %d",errno);
  190.         exit(1);
  191.     }
  192.     /* read his error message */
  193.     tcpCheckNotify();
  194.     for (;;)
  195.     {
  196.         bytes = s_read(s,line,sizeof(line)-2);
  197.         if (bytes < 0)
  198.         {
  199.             if (errno = EWOULDBLOCK)
  200.                 continue;
  201.             dprintf("read - error %d",errno);
  202.             exit(1);
  203.         }
  204.         break;
  205.     }
  206.     dprintf("read got %d bytes\n",bytes);
  207.     line[bytes] = '\0';
  208.     dprintf("'%s'\n",line);
  209.     
  210.     /* now lets be rude and quit the conversation */
  211.     tcpCheckNotify();
  212.     strcpy(line,"QUIT\015\012");
  213.     bytes = s_write(s,line,strlen(line));
  214.     if (bytes <= 0 && errno != EINPROGRESS)
  215.     {
  216.         dprintf("write - error %d",errno);
  217.     }
  218.  
  219. #ifndef LIKE_CRASHES
  220.     for (;;)
  221.     {
  222.         tcpCheckNotify();
  223.         writefds = 0xffffffff;
  224.         count = s_select(32, NULL, &writefds, NULL, &selectPoll);
  225.         if (count < 0)
  226.         {
  227.             dprintf("select - error %d",errno);
  228.             exit(1);
  229.         }
  230.         if (count > 0)
  231.             break;
  232.     }
  233.     for (;;)
  234.     {
  235.         tcpCheckNotify();
  236.         readfds = 0xffffffff;
  237.         count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
  238.         if (count < 0)
  239.         {
  240.             dprintf("select - error %d",errno);
  241.             exit(1);
  242.         }
  243.         if (count > 0)
  244.             break;
  245.     }
  246.     tcpCheckNotify();
  247.     bytes = s_read(s,line,sizeof(line)-2);
  248.     if (bytes < 0)
  249.     {
  250.         dprintf("read - error %d",errno);
  251.         exit(1);
  252.     }
  253.     dprintf("read got %d bytes\n",bytes);
  254.     line[bytes] = '\0';
  255.     dprintf("'%s'\n",line);
  256. #endif
  257.     tcpCheckNotify();
  258.     if (s_close(s) < 0)
  259.     {
  260.         dprintf("close(s) - error %d",errno);
  261.     }
  262.     
  263.     /* spin in case something odd happens at the end */
  264.     for (;;)
  265.     {
  266.         tcpCheckNotify();
  267.     }
  268. }
  269.  
  270.